package com.example.hi2_android;
import com.ibm.mqtt.MqttClient;
//import android.util.Log;

import com.ibm.mqtt.IMqttClient;
import com.ibm.mqtt.MqttException;
import com.ibm.mqtt.MqttNotConnectedException;
import com.ibm.mqtt.MqttPersistenceException;
import com.ibm.mqtt.MqttSimpleCallback;
//import com.ibm.mqtt.MqttNotConnectedException;
import com.ibm.mqtt.MqttPersistence;


//import com.ibm.mqtt.MqttPersistenceException;
//import com.ibm.mqtt.MqttSimpleCallback;

//import dalelane.android.mqtt.MQTTService;
//import dalelane.android.mqtt.MQTTService.LocalBinder;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.IBinder;
import android.util.Log;


public class hi2_service extends Service {
	
	//a unique client-id must( must be unique to the message broker)
	public static String CLIENT_ID="dalelane.mqtt";

	static //implements the connection to the message broker
	IMqttClient mqttClient=null;
	
	//host name of the server we're receiving push notifications from
    private static String          brokerHostName       = "172.16.33.142";
    private static final String		ACTION_START = CLIENT_ID + ".START";
  
  
    
    private static int BROKER_PORT_NUM=1883;
	private static MqttPersistence PERSISTENCE=null;
	private static boolean CLEAN_START=true;
	private static short KEEP_ALIVE=5;
	
	private static int[] QUALITIES_OF_SERVICE={0};
	
/*	private static int QUALITY_OF_SERVICE=0;
	private static int[] QUALITIES_OF_SERVICE={0};
	private static boolean RETAINED_PUBLISH=false;	*/
	// Static method to start the service
	private MQTTConnection			mConnection;
	
	public static void actionStart(Context ctx) {
		Intent i = new Intent(ctx, hi2_service.class);
		i.setAction(ACTION_START);
		Log.d("ClientActivity", "C: inside actionstart");
		ctx.startService(i);
	}
	
	@Override
	public void onCreate() {

		super.onCreate();				

		}
	@Override
	public void onStart(Intent intent, int startId) {		
		super.onStart(intent, startId);
		
		Log.d("ClientActivity", "C: inside onStart");
		// Do an appropriate action based on the intent.
/*		if (intent.getAction().equals(ACTION_STOP) == true) {
			stop();
			stopSelf();
		}*/ if (intent.getAction().equals(ACTION_START) == true) {
			start();
		} /*else if (intent.getAction().equals(ACTION_KEEPALIVE) == true) {
			keepAlive();
			
		} else if (intent.getAction().equals(ACTION_RECONNECT) == true) {
			if (isNetworkAvailable()) {
				reconnectIfNecessary();
			}
		}*/ 
	}
	private synchronized void start() {	
		
		Log.d("ClientActivity", "C: inside start");
		// Establish an MQTT connection
		connect();
		
	}
	private synchronized void connect() {
		try {
			mConnection = new MQTTConnection();
		} catch (MqttException e) {
			// Schedule a reconnect, if we failed to connect
			Log.d("ClientActivity", "C: inside connect");
		}
	}	
private class MQTTConnection implements MqttSimpleCallback {
		
		// Creates a new connection given the broker address and initial topic
	public MQTTConnection() throws MqttException {
	// Create connection spec
		String mqttConnSpec="tcp://"+brokerHostName+"@"+BROKER_PORT_NUM;

		try {
			Log.d("ClientActivity", "C: Connecting");
			mqttClient=MqttClient.createMqttClient(mqttConnSpec,PERSISTENCE);
			mqttClient.connect(CLIENT_ID,CLEAN_START,KEEP_ALIVE);
		}
		catch(MqttException e){
			Log.d("ClientActivity", "C: Cannot connect");
			e.printStackTrace();
		}		
		mqttClient.registerSimpleHandler(this);
		subscribeToTopic("/iisc/cs");
	}
	private void subscribeToTopic(String topicname)
	{
		Log.d("ClientActivity", "C: inside subscribe");
		try{
			String[] topics={topicname};
			mqttClient.subscribe(topics, QUALITIES_OF_SERVICE);
		}
		catch(MqttNotConnectedException e)
		{
			e.printStackTrace();
		}
		catch(IllegalArgumentException e)
		{
			e.printStackTrace();
		}
		catch(MqttException e)
		{
			e.printStackTrace();
		}
	}
	public void publishArrived(String topicname,byte[] payload,int qos,boolean retained)
	{
		Log.d("ClientActivity", "C: inside publish arrived");
	}
	@Override
	public void connectionLost() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	
}
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	
}